#pragma comment(lib, "glew32.lib")
#pragma comment(lib, "freeglut.lib")
#pragma comment(lib, "soil.lib")

#include <stdio.h>
#include <gl/glew.h>
#include <gl/glut.h>
#include <gl/soil.h>
#include <gl/freeglut_ext.h>
#include <iostream>

int windowWidth = 0;
int windowHeight = 0;


void drawCube()
{
	glBegin(GL_QUADS);
	// face v0-v1-v2-v3
	glNormal3f(0, 0, 1);
	glColor3f(1, 1, 1);
	glVertex3f(1, 1, 1);
	glColor3f(1, 1, 0);
	glVertex3f(-1, 1, 1);
	glColor3f(1, 0, 0);
	glVertex3f(-1, -1, 1);
	glColor3f(1, 0, 1);
	glVertex3f(1, -1, 1);

	// face v0-v3-v4-v6
	glNormal3f(1, 0, 0);
	glColor3f(1, 1, 1);
	glVertex3f(1, 1, 1);
	glColor3f(1, 0, 1);
	glVertex3f(1, -1, 1);
	glColor3f(0, 0, 1);
	glVertex3f(1, -1, -1);
	glColor3f(0, 1, 1);
	glVertex3f(1, 1, -1);

	// face v0-v5-v6-v1
	glNormal3f(0, 1, 0);
	glColor3f(1, 1, 1);
	glVertex3f(1, 1, 1);
	glColor3f(0, 1, 1);
	glVertex3f(1, 1, -1);
	glColor3f(0, 1, 0);
	glVertex3f(-1, 1, -1);
	glColor3f(1, 1, 0);
	glVertex3f(-1, 1, 1);

	// face  v1-v6-v7-v2
	glNormal3f(-1, 0, 0);
	glColor3f(1, 1, 0);
	glVertex3f(-1, 1, 1);
	glColor3f(0, 1, 0);
	glVertex3f(-1, 1, -1);
	glColor3f(0, 0, 0);
	glVertex3f(-1, -1, -1);
	glColor3f(1, 0, 0);
	glVertex3f(-1, -1, 1);

	// face v7-v4-v3-v2
	glNormal3f(0, -1, 0);
	glColor3f(0, 0, 0);
	glVertex3f(-1, -1, -1);
	glColor3f(0, 0, 1);
	glVertex3f(1, -1, -1);
	glColor3f(1, 0, 1);
	glVertex3f(1, -1, 1);
	glColor3f(1, 0, 0);
	glVertex3f(-1, -1, 1);

	// face v4-v7-v6-v5
	glNormal3f(0, 0, -1);
	glColor3f(0, 0, 1);
	glVertex3f(1, -1, -1);
	glColor3f(0, 0, 0);
	glVertex3f(-1, -1, -1);
	glColor3f(0, 1, 0);
	glVertex3f(-1, 1, -1);
	glColor3f(0, 1, 1);
	glVertex3f(1, 1, -1);
	glEnd();
}



void ChangeSize(int w, int h)
{
	windowWidth = w;
	windowHeight = h;

	if (h == 0)
		h = 1;
	glViewport(0, 0, w, h);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0, w*1.0 / h, 0.01, 1000.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
}

void SetupRC()
{
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glPixelStorei(GL_PACK_ALIGNMENT, 1);
	glPixelStorei(GL_PACK_SWAP_BYTES, TRUE);
}

void RenderScene(void)
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	glLoadIdentity();
	glTranslated(0, 0, -2);
	drawCube();
	 
	glutSwapBuffers();
}


void Keyboard(unsigned char key, int x, int y)
{
	
	if (key == 'a')
	{
		unsigned char* pixels = new unsigned char[windowWidth*windowHeight * 4];
		glReadPixels(0, 0, windowWidth, windowHeight, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

		
		SOIL_save_image("hello.bmp", SOIL_SAVE_TYPE_BMP, windowWidth, windowHeight, SOIL_LOAD_RGBA, pixels);

		//int
		//	SOIL_save_image
		//	(
		//		const char *filename,
		//		int image_type,
		//		int width, int height, int channels,
		//		const unsigned char *const data
		//	)


		//if (key == 's')
		//{
		//	glDrawPixels(windowWidth, windowHeight, GL_RGBA, GL_UNSIGNED_BYTE, pixels);
		//}
	}


	
}


int main(int argc, char* argv[])
{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_STENCIL);
	glutInitWindowSize(800, 600);
	glutCreateWindow("OpenGL");
	glutReshapeFunc(ChangeSize);
	glutDisplayFunc(RenderScene);
	glutKeyboardFunc(Keyboard);

	GLenum err = glewInit();
	if (GLEW_OK != err) {
		fprintf(stderr, "GLEW Error: %s\n", glewGetErrorString(err));
		return 1;
	}

	SetupRC();

	glutMainLoop();
	return 0;
}